//Page Object
Page({
  data: {
    address: {},
    cart: [],
    allCheck: true,
    allPrice: 0,
  },
  getCart(cart2) {
    let cart = cart2;
    cart.pics = [];
    cart.pics = cart2.pics[0];
    const allCheck = cart.length ? cart.every(v => v.checked) : false;
    let allPrice = 0;
    cart.forEach(item => {
      if (item.checked) {
        allPrice += item.goods_price * item.goods_num;
      }
    })
    this.setData({
      allPrice,
    })
    this.setData({
      cart,
      allCheck,
    })
    wx.setStorageSync("cart", cart2);
  },
  onShow() {
    let address = wx.getStorageSync("address") || {};
    if (JSON.stringify(address) === '{}') {
    } else {
      let cart2 = wx.getStorageSync("cart") || [];
      address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
      this.setData({
        address,
      })
      this.getCart(cart2);
    }
  },
  handlebtn() {
    wx.chooseAddress({
      success: (result) => {
        wx.setStorageSync("address", result);
        let address = result;
        console.log(address)
        address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;
        this.setData({
          address,
        })
      },
    });
  },
  handleItemAllCheck(e) {
    let cart2 = wx.getStorageSync("cart") || [];
    if (JSON.stringify(e.detail.value) === "[]") {
      cart2.forEach(item => {
        item.checked = false;
      })
    } else {
      cart2.forEach(item => {
        item.checked = true;
      })
    }
    this.getCart(cart2)
  },
  checkedItem(e){
    console.log(e)
    let operation = e.currentTarget.dataset.operation;
    let cart2 = wx.getStorageSync("cart") || [];
    if(JSON.stringify(e.detail.value) === '[]'){
      cart2.forEach(item => {
        if(item.cat_id === operation){
          item.checked = false;
        }
      })
    }else {
      cart2.forEach(item => {
        if(item.cat_id === operation){
          item.checked = true;
        }
      })
    }
    this.getCart(cart2)
  },
  bindedit(e){
    let operation = e.currentTarget.dataset.operation;
    let operate = e.currentTarget.dataset.operate;
    let de = e.currentTarget.dataset.delete;
    let cart2 = wx.getStorageSync("cart") || [];
    if(JSON.stringify(operation) === "true"){
      cart2.forEach(item => {
        if(item.cat_id === operate){
          item.goods_num++;
        }
      })
    }else {
      cart2.forEach(item => {
        if(item.cat_id === operate){
          if(item.goods_num === 1){
            wx.showModal({
              title: '',
              content: '是否删除商品',
              showCancel: true,
              success: (result) => {
                if(result.confirm){
                  cart2.splice(de,1);
                  this.getCart(cart2);
                }
              },
              fail: ()=>{},
              complete: ()=>{}
            });
          }else {
            item.goods_num--;
          }
        }
      })
    }
    this.getCart(cart2);
  },
  jiesuan(){
    if(!this.data.address){
      wx.showToast({
        title: '您还没添加地址',
      });
      return ;
    }
    else if(this.data.cart.length<1){
      wx.showToast({
        title: '您还没添加商品',
      });
      return ;
    }
    else {
      wx.navigateTo({
        url: '../pay/index',
        success: (result)=>{
          
        },
        fail: ()=>{},
        complete: ()=>{}
      });
    }
   
  }
});